<template>
	<view class="yuyueinfo">
		<headertop title="租赁信息" str1="oridei" :back1="back1"></headertop>
		<view class="topimage">

		</view>
		<view class="infobox" v-if="typestr!='duoxuan'">
			<image :src="contentobj.image" class="leftimg" mode=""></image>
			<view class="texts">
				<view class="t1">
					<view class="title">
						{{contentobj.title}}
					</view>
				</view>
				<view class="t2">
					{{contentobj.detail}}
				</view>
				<view class="t3line">
					<view class="leftjine">
						押金：￥{{contentobj.deposit}}
					</view>
					<view class="rightzupingfei">
						<view class="leftjine">
							租赁费：
						</view>
						<view class="jine">
							￥{{contentobj.price}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="" style="margin-top: 186rpx;" v-if="typestr=='duoxuan'">
			<view class="infobox" v-for="item in zuliarr" style="margin-bottom: 30rpx;margin-top:0rpx !important;"
				:key="item.id">
				<image :src="item.image" class="leftimg" mode=""></image>
				<view class="texts">
					<view class="t1">
						<view class="title">
							{{item.title}}
						</view>
					</view>
					<view class="t2">
						{{item.detail}}
					</view>
					<view class="t3line">
						<view class="leftjine">
							押金：￥{{item.deposit}}
						</view>
						<view class="rightzupingfei">
							<view class="leftjine">
								租赁费：
							</view>
							<view class="jine">
								￥{{item.price}}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="yuyuebox">
			<view class="center">
				<view class="title">
					<view class="leftbox">

					</view>
					<view class="tt">
						租用人信息
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						<span style=" color: #ff0000">*</span>联系人:
					</view>
					<view class="righttitle">
						<input v-model="query.username" type="text" placeholder="请输入预约人姓名 (必填)" />
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						<span style=" color: #ff0000">*</span>联系方式:
					</view>
					<view class="righttitle">
						<input v-model="query.telphone" type="text" placeholder="请输入手机号码 (必填)" />
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						备注:
					</view>
					<view class="righttitle">
						<input v-model="query.memo" type="text" placeholder="请输入备注" />
					</view>
				</view>
			</view>
		</view>
		<view class="yuyuebox">
			<view class="center">
				<view class="title">
					<view class="leftbox">

					</view>
					<view class="tt">
						服务信息
					</view>
				</view>
				<view class="linebox" @click="show1=true">
					<view class="labletitle">
						服务类型:
					</view>
					<view class="righttitle">
						{{ydtypearr[query.ydtype-1]['name']}}
					</view>
					<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Frame@2x(46).png" class="bianji"
						mode=""></image>
				</view>
				<view class="linebox">
					<view class="labletitle">
						<span style=" color: #ff0000">*</span>营位信息:
					</view>
					<view class="righttitle" v-if="ydtypearr[query.ydtype-1]['name']=='预订'" @click="ff">
						{{query.campfile||'无'}}
					</view>
					<view class="righttitle" v-else>
						<input type="text" placeholder="请输入营位信息" v-model="query.campfile" />
					</view>
					<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Frame@2x(46).png" class="bianji"
						mode="" v-if="arrcampfile.length>1&&ydtypearr[query.ydtype-1]['name']=='预订'" @click="ff">
					</image>
				</view>
			</view>
		</view>
		<view class="yuyuebox">
			<view class="center">
				<view class="title">
					<view class="leftbox">

					</view>
					<view class="tt">
						费用信息
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						租赁费:
					</view>
					<view class="righttitle" v-if="typestr!='duoxuan'">
						￥{{contentobj.price}}
					</view>
					<view class="righttitle" v-if="typestr=='duoxuan'">
						￥{{cdff}}
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						押金:
					</view>
					<view class="righttitle" v-if="typestr!='duoxuan'">
						￥{{contentobj.deposit}}
					</view>
					<view class="righttitle" v-else>
						￥{{yjff}}
					</view>
				</view>
			</view>
		</view>
		<view class="yuyuebox">
			<view class="center">
				<view class="title">
					<view class="leftbox">

					</view>
					<view class="tt">
						支付方式
					</view>
				</view>
				<view class="linebox1" @click="active=0">
					<view class="lefttext">
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11200@2x.png"
							class="iconsmall" mode="">
						</image>
						<span>微信支付</span>
					</view>
					<image
						:src="active==0?'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11457@2x.png':'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Ellipse 2638@2x.png'"
						class="xz" mode=""></image>
				</view>
				<view class="linebox1" @click="active=1">
					<view class="lefttext">
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11428@2x.png"
							class="iconsmall" mode="">
						</image>
						<span>余额支付（可用￥{{yueobj.money||0}}）</span>
					</view>
					<view class="right">
						<span style="margin-right:18rpx ;"
							v-if="typestr!='duoxuan'">{{(contentobj.deposit*1+contentobj.price*1)>yueobj.money?'余额不足':''}}</span>
						<span style="margin-right:18rpx ;" v-else>{{(cdff+yjff)>yueobj.money?'余额不足':''}}</span>
						<image
							:src="active==1?'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11457@2x.png':'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Ellipse 2638@2x.png'"
							class="xz" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view class="xieyitext" @click="flag=!flag">
			<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Ellipse 31@2x.png" v-if="!flag" mode="">
			</image>
			<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11432@2x.png" v-else class="xz"
				mode=""></image>
			<view class="text" @click="goxieyi">
				勾选阅读并同意《{{xiyi['title']}}》
			</view>
		</view>
		<view class="jge" style="height: 190rpx;">
			<up-popup :show="show" mode="center">
				<view class="popucenter">
					<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/图层_10@2x.png" class="popucenterimg"
						mode=""></image>
					<view class="title">
						支付成功！
					</view>
					<view class="text">
						您的租赁付款已成功，
					</view>
					<view class="text">
						订单正在处理中。
					</view>
					<view class="btn" @click="gopage()">
						返回首页
					</view>
					<view class="lookorder" @click="lookorder">
						查看订单
					</view>
				</view>
			</up-popup>
		</view>


		<up-popup :show="show1" mode="bottom">
			<view class="xuanxiang" @click="show1=false">
				<view class="tt" style="margin-left: 30rpx;color: #888;">
					取消
				</view>
				<view class="tt" style="font-weight: 600;">
					请选择服务类型
				</view>
				<view class="tt" style="margin-right: 30rpx;color: #0077BE;" @click="queding">
					确认
				</view>
			</view>
			<picker-view :indicator-style="indicatorStyle" @change="bindChange" class="picker-view">
				<picker-view-column>
					<view class="item" v-for="(item,index) in ydtypearr" :key="index">{{item.name}}</view>
				</picker-view-column>
			</picker-view>
		</up-popup>
		<up-popup :show="show2" mode="bottom">
			<view class="xuanxiang" @click="show2=false">
				<view class="tt" style="margin-left: 30rpx;color: #888;">
					取消
				</view>
				<view class="tt" style="font-weight: 600;">
					请选择营位
				</view>
				<view class="tt" style="margin-right: 30rpx;color: #0077BE;" @click="queding1">
					确认
				</view>
			</view>
			<picker-view :indicator-style="indicatorStyle" @change="bindChange1" class="picker-view">
				<picker-view-column>
					<view class="item" v-for="(item,index) in arrcampfile" :key="index">{{item.cname}}</view>
				</picker-view-column>
			</picker-view>
		</up-popup>
		<sanshui-payment-password :jiage="feiyong" :val="payFlag" :digits="6" @submit="onSubmit" @cancel="onCancel" />
		<view class="fixedbox">
			<view class="btn" @click="qrzp">
				确认租赁
			</view>
		</view>
	</view>
</template>

<script setup>
	import SanshuiPaymentPassword from '@/components/sanshui-payment-password/sanshui-payment-password.vue'
	import {
		onPageScroll,
		onLoad
	} from "@dcloudio/uni-app";
	import {
		leasefile,
		myyingwei,
		addorder
	} from '@/api/lease.js'
	import {
		article
	} from '@/api/home.js'
	import {
		reactive,
		ref,
		computed
	} from 'vue';
	import {
		walletlog
	} from '@/api/my.js'
	import headertop from '@/components/header.vue'
	const active1 = ref(0)
	const active = ref(-1)
	const payFlag = ref(false)
	const paypwd = ref('')
	const back1 = ref('')
	const show = ref(false)
	const flag = ref(false)
	const yueobj = ref({})
	const show1 = ref(false)
	const show2 = ref(false)
	const arrcampfile = ref([])
	const active2 = ref(0)
	const payoid = ref('')
	const indicatorStyle = ref(`height: 50px;`)
	const contentobj = ref({})
	const typestr = ref('')
	const xiyi = ref({})
	const zuliarr = ref([])
	const query = reactive({
		lid: '',
		username: '',
		telphone: '',
		memo: '',
		ydtype: 1,
		campfile: '',
		paytype: ''
	})
	const feiyong = computed(() => {
		if (typestr.value == 'duoxuan') {
			var jiage = cdff.value + yjff.value
			return jiage
		} else {
			var jiage = ''
			jiage = contentobj.value.price * 1 + contentobj.value.deposit * 1
			return jiage
		}
	})

	const cdff = computed(() => {
		var jiage = zuliarr.value.reduce((e, item) => {
			return e + item.price * 1
		}, 0)
		console.log(jiage, 'jiagejiagejiagejiagejiagejiagejiagejiagejiagejiage');
		return jiage.toFixed(2)
	})

	function onSubmit(e) {
		console.log(e, '输入密码到指定位数的时候自动触发的函数');
		payFlag.value = false
		paypwd.value = e.value
		qrzp()
	}

	function onCancel() {
		payFlag.value = false
		console.log('关闭');
	}
	var yjff = computed(() => {
		var jiage = zuliarr.value.reduce((e, item) => {
			return e + item.deposit * 1
		}, 0)
		console.log(jiage, 'jiagejiagejiagejiagejiagejiagejiagejiagejiagejiage');
		return jiage.toFixed(2)
	})

	const ydtypearr = ref([{
		'name': '堂食类',
		id: 2
	}, {
		'name': '营位类',
		id: 3
	}, {
		'name': '自取类',
		id: 4
	}])
	onPageScroll((e) => {
		console.log(e, 'e.scrollTope.scrollTope.scrollTop');
		if (e.scrollTop > 50) {
			back1.value = '#9DCA95'
		} else {
			back1.value = ''
		}
	})
	onLoad((opt) => {
		paypwd.value = ''
		query.lid = opt.id
		if (opt.typestr == 'duoxuan') {
			typestr.value = opt.typestr
			zuliarr.value = uni.getStorageSync('zuliarr')
		} else {
			init(opt.id)
		}
		initxieyi()
		campfileinit()
		myyue()
	})

	async function initxieyi() {
		const res = await article({
			'id': 8
		})
		if (res.data.code == 1) {
			xiyi.value = res.data.data
		}
	}

	function goxieyi() {
		uni.navigateTo({
			url: '/sub_index/index/activedetail?title=租赁协议'
		})
	}
	async function myyue() {
		const res = await walletlog()
		if (res.data.code == 1) {
			yueobj.value = res.data.data
		}
	}
	async function qrzp() {
		console.log(query, 'queryqueryquery');
		if (active.value == -1) {
			uni.showToast({
				title: '请选择支付方式',
				icon: 'none'
			})
			return
		}
		query.paytype = active.value + 1
		for (var i in query) {
			if (i != 'memo' && i != 'lid') {
				if (!query[i]) {
					uni.showToast({
						title: '请补全租用人信息和服务信息',
						icon: 'none'
					})
					return
				}
			}
		}
		if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(query.telphone))) { //校验手机号码是否有误
			uni.showToast({
				title: '请填写正确手机号码',
				icon: "none"
			});
			return;
		}
		if (!flag.value) {
			uni.showToast({
				title: '请勾选租赁协议',
				icon: 'none'
			})
			return
		}

		if (active.value == -1) {
			uni.showToast({
				title: '请选择支付方式',
				icon: 'none'
			})
			return
		}
		var lid = ''
		if (typestr.value == 'duoxuan') {
			var ids = zuliarr.value.map(item => item.id)
			lid = ids.join()
		} else {
			lid = contentobj.value.id
		}
		if (active.value != 0) {
			if (!paypwd.value) {
				payFlag.value = true
				return
			}
		}

		const res = await addorder({
			lid,
			username: query.username,
			telphone: query.telphone,
			memo: query.memo,
			ydtype: query.ydtype,
			paytype: query.paytype,
			paypwd: paypwd.value,
			campfile: query.campfile,
		})
		if (res.code == 0) {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
			return
		}
		if (res.data.code == 1) {
			payoid.value = res.data.data.oid
			if (active.value == 0) {
				var payobj = res.data.data.pay
				uni.requestPayment({
					timeStamp: String(payobj.timestamp),
					nonceStr: payobj.noncestr,
					package: payobj.package,
					signType: 'MD5',
					paySign: payobj.sign,
					success(res) {
						show.value = true

					},
					fail(e) {
						setTimeout(() => {
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
						}, 500)
						uni.navigateTo({
							url: '/sub_my/my/zupingorderdetail?id=' + payoid.value
						})
						// console.log('支付失败--------------', e);
					}
				})
			} else {
				show.value = true
			}
		} else {
			uni.showToast({
				title: res.data.msg,
				icon: 'none'
			})
		}

	}

	function lookorder() {
		uni.navigateTo({
			url: '/sub_my/my/zupingorderdetail?id=' + payoid.value
		})
	}

	async function campfileinit() {
		const res = await myyingwei()
		if (res.data.code == 1) {

			query.campfile = res.data.data.cname[0]['cname']
			arrcampfile.value = res.data.data.cname
			if (arrcampfile.value.length) {
				ydtypearr.value = [{
					'name': '预订',
					id: 1
				}, {
					'name': '堂食类',
					id: 2
				}, {
					'name': '营位类',
					id: 3
				}, {
					'name': '自取类',
					id: 4
				}]
			} else {
				ydtypearr.value = [{
					'name': '堂食类',
					id: 2
				}, {
					'name': '营位类',
					id: 3
				}, {
					'name': '自取类',
					id: 4
				}]
			}
		}
	}

	function queding1() {
		console.log(active2.value, 'active2.value,active2.value,active2.value,');
		if (active2.value == 0) {
			active2.value = arrcampfile.value[0]['cname']
		}
		query.campfile = active2.value
		show2.value = false
	}

	function queding() {
		query.ydtype = active1.value + 1
		show1.value = false
	}

	function bindChange(e) {
		const val = e.detail.value
		console.log(val, 'val-----------------');
		active1.value = val[0]
	}
	async function init(id) {
		const res = await leasefile({
			id
		})
		if (res.data.code == 1) {
			contentobj.value = res.data.data
		}
	}

	function bindChange1(e) {
		const val = e.detail.value
		console.log(val, 'val-----------------');
		active2.value = arrcampfile.value[val[0]]['cname']
	}

	function ff() {
		if (arrcampfile.value.length > 1) {
			show2.value = true
		}
	}

	function gopage() {
		uni.switchTab({
			url: '/pages/index/index'
		})
	}
</script>

<style lang="scss">
	.xieyitext {
		width: 686rpx;
		display: flex;
		margin: 44rpx auto;
		align-items: center;

		image {
			width: 32rpx;
			height: 32rpx;
		}

		.text {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #4E5969;
			margin-left: 14rpx;
		}
	}

	.xuanxiang {
		width: 100%;
		display: flex;
		align-items: center;
		margin-top: 30rpx;
		overflow: hidden;
		justify-content: space-between;
		margin-bottom: 30rpx;

		.tt {
			font-size: 32rpx;
		}
	}

	.jge {
		::v-deep .u-popup__content {
			background-color: rgba(255, 0, 0, 0) !important;
		}
	}


	.picker-view {
		width: 750rpx;
		height: 400rpx;
		margin-top: 20rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0 0;
	}

	.item {
		line-height: 100rpx;
		text-align: center;
	}

	::v-deep .u-popup__content {
		// background-color: rgba(255, 0, 0, 0) !important;
		border-radius: 20rpx 20rpx 0 0;
	}

	.popucenter {
		width: 592rpx;
		height: 720rpx;
		background: linear-gradient(180deg, #F4FFF5 0%, #FFFFFF 31%);
		border-radius: 28rpx 28rpx 28rpx 28rpx;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		align-items: center;

		.popucenterimg {
			width: 355.92rpx;
			height: 315.24rpx;
			margin-top: 22rpx;
		}

		.title {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #1D2129;
			margin-bottom: 24rpx;
		}

		.text {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #4E5969;
		}

		.btn {
			width: 422rpx;
			height: 64rpx;
			background: #7EBC79;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			text-align: center;
			line-height: 64rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #FFFFFF;
			margin-top: 22rpx;
		}

		.lookorder {
			width: 422rpx;
			height: 64rpx;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			border: 2rpx solid #7EBC79;
			text-align: center;
			line-height: 64rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			margin-top: 24rpx;
			color: #7EBC79;
		}
	}

	.fixedbox {
		width: 100%;
		overflow: hidden;
		height: 184rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -4rpx 4rpx 0rpx rgba(0, 0, 0, 0.08);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		position: fixed;
		bottom: 0%;
		z-index: 99;

		.btn {
			width: 686rpx;
			height: 88rpx;
			background: #7EBC79;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			text-align: center;
			line-height: 88rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 88rpx;
			margin: auto;
			margin-top: 22rpx;
		}
	}

	.yuyueinfo {
		width: 100%;
		overflow: hidden;
		position: relative;

		.topimage {
			width: 100%;
			height: 530rpx;
			background: linear-gradient(180deg, #84BD78 0%, rgba(132, 189, 120, 0) 100%);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			position: absolute;
			top: 0%;
			z-index: -1;
		}

		.yuyuebox {
			width: 686rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.15);
			border-radius: 24rpx 24rpx 24rpx 24rpx;
			margin: auto;
			margin-top: 32rpx;
			overflow: hidden;

			.center {
				width: 630rpx;
				margin: auto;

				.linebox1 {
					width: 100%;
					height: 84rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.xz {
						width: 32rpx;
						height: 32rpx;
					}

					.right {
						display: flex;
						font-size: 24rpx;
						color: #86909C;
						align-items: center;
					}

					.lefttext {
						display: flex;
						align-items: center;
						font-weight: 400;
						font-size: 28rpx;
						color: #1D2129;

						.iconsmall {
							width: 40rpx;
							height: 40rpx;
							margin-right: 12rpx;
						}
					}
				}

				.linebox {
					width: 100%;
					min-height: 84rpx;
					display: flex;
					align-items: center;
					border-bottom: 2rpx solid #F7F8FA;
					position: relative;


					.labletitle {
						width: 140rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #1D2129;
					}

					.righttitle {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #1D2129;
						position: relative;
						display: flex;
						align-items: center;
						margin-left: 20rpx;
						width: 500rpx;
					}

					.bianji {
						right: 0%;
						width: 32rpx;
						height: 32rpx;
						position: absolute;
					}
				}

				.title {
					width: 100%;
					display: flex;
					align-items: center;
					margin-top: 34rpx;
					margin-bottom: 20rpx;

					.leftbox {
						width: 6rpx;
						height: 36rpx;
						background: #7EBC79;
						border-radius: 0rpx 24rpx 24rpx 0rpx;
					}

					.tt {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 32rpx;
						color: #1D2129;
						margin-left: 12rpx;
					}
				}
			}
		}

		.infobox {
			width: 686rpx;
			height: 224rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 4rpx 0rpx rgba(134, 202, 115, 0.43);
			border-radius: 24rpx 24rpx 24rpx 24rpx;
			margin: auto;
			margin-top: 186rpx;
			display: flex;
			align-items: center;

			.leftimg {
				width: 198rpx;
				height: 176rpx;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				margin-left: 24rpx;
			}

			.texts {
				width: 412rpx;
				margin-left: 24rpx;
				overflow: hidden;

				.t1 {
					width: 412rpx;
					display: flex;
					align-items: center;
					margin-top: 16rpx;

					.leftpobox {
						width: 56rpx;
						height: 40rpx;
						background: #7EBC79;
						border-radius: 4rpx 20rpx 4rpx 20rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 32rpx;
						color: #FFFFFF;
					}

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 32rpx;
						color: #1D2129;
						margin-left: 8rpx;
					}
				}

				.t3line {
					display: flex;
					align-items: center;
					margin-top: 32rpx;
					flex-direction: row;
					white-space: normal;

					.leftjine {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #86909C;
					}

					.rightzupingfei {
						// margin-left: 30rpx;
						display: flex;

						.leftjine {
							line-height: 50rpx;
							margin-left: 20rpx;
						}

						.jine {
							font-family: PingFang SC, PingFang SC;
							font-weight: bold;
							font-size: 30rpx;
							color: #7EBC79;
						}
					}
				}

				.t2 {
					width: 412rpx;
					white-space: nowrap;
					overflow: hidden;
					font-weight: 400;
					font-size: 26rpx;
					color: #868686;
					margin-top: 26rpx;
					text-overflow: ellipsis;
				}
			}
		}
	}
</style>